package com.zxiw.zxedu.recyclerview.adapter

import android.animation.Animator
import android.content.Context
import android.view.View
import android.view.animation.Interpolator


/**
 * @author zhangxiaoming
 * @date 2023/8/18
 * @description 继承于HelperRecyclerViewAdapter提供Itme动画效果便捷操作的baseAdapter
 */
abstract class HelperRecyclerViewAnimAdapter<T>(context: Context, vararg layoutId: Int) :
    HelperRecyclerViewAdapter<T>(context, *layoutId) {
    private var animationType: AnimationType? = null
    private var mAnimationDuration: Long = 300
    private var showItemAnimationEveryTime = false
    private var mItemAnimationInterpolator: Interpolator? = null
    private var mCustomAnimator: CustomAnimator? = null
    private var mLastItemPosition = -1

    override fun onBindViewHolder(holder: BH, position: Int) {
        super.onBindViewHolder(holder, position)
        addAnimation(holder);
    }

    protected fun addAnimation(holder: BH) {
        val currentPosition = holder.adapterPosition
        if (null != mCustomAnimator) {
            mCustomAnimator?.getAnimator(holder.itemView)?.duration = mAnimationDuration
        } else if (animationType != null) {
            if (showItemAnimationEveryTime || currentPosition > mLastItemPosition) {
                AnimationUtil().apply {
                    this.mAnimationType = animationType!!
                    mTargetView = holder.itemView
                    mDuration = mAnimationDuration
                    mInterpolator = mItemAnimationInterpolator
                }.start()
                mLastItemPosition = currentPosition
            }
        }
    }

    fun setItemAnimation(animationType: AnimationType) {
        this.animationType = animationType
    }

    fun setItemAnimationDuration(animationDuration: Long) {
        mAnimationDuration = animationDuration
    }

    fun setItemAnimationInterpolator(animationInterpolator: Interpolator) {
        mItemAnimationInterpolator = animationInterpolator
    }

    fun setShowItemAnimationEveryTime(showItemAnimationEveryTime: Boolean) {
        this.showItemAnimationEveryTime = showItemAnimationEveryTime
    }

    fun setCustomItemAnimator(customAnimator: CustomAnimator) {
        mCustomAnimator = customAnimator
    }

    interface CustomAnimator {
        fun getAnimator(itemView: View): Animator
    }
}